package lego.diagram.providers;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import lego.LegoPackage;
import lego.diagram.edit.parts.ActionerPortActionerEditPart;
import lego.diagram.edit.parts.ActionerPortEditPart;
import lego.diagram.edit.parts.LightSensorEditPart;
import lego.diagram.edit.parts.MicroSensorEditPart;
import lego.diagram.edit.parts.MotorActionerEditPart;
import lego.diagram.edit.parts.PicoNetEditPart;
import lego.diagram.edit.parts.PicoNetMasterEditPart;
import lego.diagram.edit.parts.PicoNetSlavesEditPart;
import lego.diagram.edit.parts.RobotEditPart;
import lego.diagram.edit.parts.RootEditPart;
import lego.diagram.edit.parts.SensorPortEditPart;
import lego.diagram.edit.parts.SensorPortSensorEditPart;
import lego.diagram.edit.parts.TouchSensorEditPart;
import lego.diagram.edit.parts.UltraSoundSensorEditPart;
import lego.diagram.part.LegoDiagramEditorPlugin;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

/**
 * @generated
 */
public class LegoElementTypes {

	/**
	 * @generated
	 */
	private LegoElementTypes() {
	}

	/**
	 * @generated
	 */
	private static Map<IElementType, ENamedElement> elements;

	/**
	 * @generated
	 */
	private static ImageRegistry imageRegistry;

	/**
	 * @generated
	 */
	private static Set<IElementType> KNOWN_ELEMENT_TYPES;

	/**
	 * @generated
	 */
	public static final IElementType Root_1000 = getElementType("lego.diagram.Root_1000"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType MicroSensor_2001 = getElementType("lego.diagram.MicroSensor_2001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType PicoNet_2002 = getElementType("lego.diagram.PicoNet_2002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType UltraSoundSensor_2003 = getElementType("lego.diagram.UltraSoundSensor_2003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType TouchSensor_2004 = getElementType("lego.diagram.TouchSensor_2004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType LightSensor_2005 = getElementType("lego.diagram.LightSensor_2005"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Robot_2006 = getElementType("lego.diagram.Robot_2006"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType MotorActioner_2007 = getElementType("lego.diagram.MotorActioner_2007"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType ActionerPort_3001 = getElementType("lego.diagram.ActionerPort_3001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType SensorPort_3002 = getElementType("lego.diagram.SensorPort_3002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType SensorPortSensor_4001 = getElementType("lego.diagram.SensorPortSensor_4001"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static final IElementType PicoNetMaster_4002 = getElementType("lego.diagram.PicoNetMaster_4002"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static final IElementType PicoNetSlaves_4003 = getElementType("lego.diagram.PicoNetSlaves_4003"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static final IElementType ActionerPortActioner_4004 = getElementType("lego.diagram.ActionerPortActioner_4004"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}
		return imageRegistry;
	}

	/**
	 * @generated
	 */
	private static String getImageRegistryKey(ENamedElement element) {
		return element.getName();
	}

	/**
	 * @generated
	 */
	private static ImageDescriptor getProvidedImageDescriptor(
			ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass
					&& !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return LegoDiagramEditorPlugin.getInstance()
						.getItemImageDescriptor(
								eClass.getEPackage().getEFactoryInstance()
										.create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(ENamedElement element) {
		String key = getImageRegistryKey(element);
		ImageDescriptor imageDescriptor = getImageRegistry().getDescriptor(key);
		if (imageDescriptor == null) {
			imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
		}
		return imageDescriptor;
	}

	/**
	 * @generated
	 */
	public static Image getImage(ENamedElement element) {
		String key = getImageRegistryKey(element);
		Image image = getImageRegistry().get(key);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
			image = getImageRegistry().get(key);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImageDescriptor(element);
	}

	/**
	 * @generated
	 */
	public static Image getImage(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImage(element);
	}

	/**
	 * Returns 'type' of the ecore object associated with the hint.
	 * 
	 * @generated
	 */
	public static ENamedElement getElement(IAdaptable hint) {
		Object type = hint.getAdapter(IElementType.class);
		if (elements == null) {
			elements = new IdentityHashMap<IElementType, ENamedElement>();

			elements.put(Root_1000, LegoPackage.eINSTANCE.getRoot());

			elements.put(MicroSensor_2001,
					LegoPackage.eINSTANCE.getMicroSensor());

			elements.put(PicoNet_2002, LegoPackage.eINSTANCE.getPicoNet());

			elements.put(UltraSoundSensor_2003,
					LegoPackage.eINSTANCE.getUltraSoundSensor());

			elements.put(TouchSensor_2004,
					LegoPackage.eINSTANCE.getTouchSensor());

			elements.put(LightSensor_2005,
					LegoPackage.eINSTANCE.getLightSensor());

			elements.put(Robot_2006, LegoPackage.eINSTANCE.getRobot());

			elements.put(MotorActioner_2007,
					LegoPackage.eINSTANCE.getMotorActioner());

			elements.put(ActionerPort_3001,
					LegoPackage.eINSTANCE.getActionerPort());

			elements.put(SensorPort_3002, LegoPackage.eINSTANCE.getSensorPort());

			elements.put(SensorPortSensor_4001,
					LegoPackage.eINSTANCE.getSensorPort_Sensor());

			elements.put(PicoNetMaster_4002,
					LegoPackage.eINSTANCE.getPicoNet_Master());

			elements.put(PicoNetSlaves_4003,
					LegoPackage.eINSTANCE.getPicoNet_Slaves());

			elements.put(ActionerPortActioner_4004,
					LegoPackage.eINSTANCE.getActionerPort_Actioner());
		}
		return (ENamedElement) elements.get(type);
	}

	/**
	 * @generated
	 */
	private static IElementType getElementType(String id) {
		return ElementTypeRegistry.getInstance().getType(id);
	}

	/**
	 * @generated
	 */
	public static boolean isKnownElementType(IElementType elementType) {
		if (KNOWN_ELEMENT_TYPES == null) {
			KNOWN_ELEMENT_TYPES = new HashSet<IElementType>();
			KNOWN_ELEMENT_TYPES.add(Root_1000);
			KNOWN_ELEMENT_TYPES.add(MicroSensor_2001);
			KNOWN_ELEMENT_TYPES.add(PicoNet_2002);
			KNOWN_ELEMENT_TYPES.add(UltraSoundSensor_2003);
			KNOWN_ELEMENT_TYPES.add(TouchSensor_2004);
			KNOWN_ELEMENT_TYPES.add(LightSensor_2005);
			KNOWN_ELEMENT_TYPES.add(Robot_2006);
			KNOWN_ELEMENT_TYPES.add(MotorActioner_2007);
			KNOWN_ELEMENT_TYPES.add(ActionerPort_3001);
			KNOWN_ELEMENT_TYPES.add(SensorPort_3002);
			KNOWN_ELEMENT_TYPES.add(SensorPortSensor_4001);
			KNOWN_ELEMENT_TYPES.add(PicoNetMaster_4002);
			KNOWN_ELEMENT_TYPES.add(PicoNetSlaves_4003);
			KNOWN_ELEMENT_TYPES.add(ActionerPortActioner_4004);
		}
		return KNOWN_ELEMENT_TYPES.contains(elementType);
	}

	/**
	 * @generated
	 */
	public static IElementType getElementType(int visualID) {
		switch (visualID) {
		case RootEditPart.VISUAL_ID:
			return Root_1000;
		case MicroSensorEditPart.VISUAL_ID:
			return MicroSensor_2001;
		case PicoNetEditPart.VISUAL_ID:
			return PicoNet_2002;
		case UltraSoundSensorEditPart.VISUAL_ID:
			return UltraSoundSensor_2003;
		case TouchSensorEditPart.VISUAL_ID:
			return TouchSensor_2004;
		case LightSensorEditPart.VISUAL_ID:
			return LightSensor_2005;
		case RobotEditPart.VISUAL_ID:
			return Robot_2006;
		case MotorActionerEditPart.VISUAL_ID:
			return MotorActioner_2007;
		case ActionerPortEditPart.VISUAL_ID:
			return ActionerPort_3001;
		case SensorPortEditPart.VISUAL_ID:
			return SensorPort_3002;
		case SensorPortSensorEditPart.VISUAL_ID:
			return SensorPortSensor_4001;
		case PicoNetMasterEditPart.VISUAL_ID:
			return PicoNetMaster_4002;
		case PicoNetSlavesEditPart.VISUAL_ID:
			return PicoNetSlaves_4003;
		case ActionerPortActionerEditPart.VISUAL_ID:
			return ActionerPortActioner_4004;
		}
		return null;
	}

}
